package model.semantics;

import model.State;

public class RepeatNode extends Node<Integer> {
	private AggregatorNode<Integer> myAggregator;
	private Node<Integer> myNumTimesNode;
	
	public RepeatNode(Node<Integer> numTimesNode, AggregatorNode<Integer> aggregator) {
		myAggregator = aggregator;
		myNumTimesNode = numTimesNode;
	}
	
	@Override
	public Integer evaluate(State<Integer> state) {
		Integer output = null;
		int numTimes = myNumTimesNode.evaluate(state);
		for(int i = 0; i < numTimes; i++) {
			output = myAggregator.evaluate(state);
		}
		
		return output;
	}

	@Override
	public String toString() {
		return String.format("(repeat %s %s)", myNumTimesNode.toString(), myAggregator.toString());
	}
}
